﻿using System.Data.Entity.ModelConfiguration;

namespace JHT.EFMigrationTest.ClassLibrary1
{
    internal class StudentsMap : EntityTypeConfiguration<Students>
    {
        public StudentsMap()
        {

            Map(m =>
            {
                m.Properties(p => new { p.Id, p.StudentName, p.AssignedClassId, p.Gender });
                m.ToTable("Students");
                HasKey(p => p.Id);
                Property(p => p.Id).HasColumnName("Id").HasColumnType("bigint").HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity).IsRequired();
                Property(p => p.StudentName).HasColumnName("StudentName").HasColumnType("varchar").HasMaxLength(50);
                Property(p => p.AssignedClassId).HasColumnName("AssignedClassId").HasColumnType("bigint").IsOptional();
                Property(p => p.Gender).HasColumnType("Gender").HasColumnType("int").IsOptional();
                Property(p => p.AssignedClassId).HasColumnName("AssignedClassId").HasColumnType("bigint").IsOptional();
                HasRequired(p => p.ClassInfo).WithMany(t => t.Students).HasForeignKey(c => c.AssignedClassId).WillCascadeOnDelete(false);// 指定外键关系 一对多
                // HasOptional(p => p.Address).WithRequired(p => p.Students);//建立外键关系一对多 在主表建立外键关系  
            });

            Map(m =>
            {
                HasKey(p => p.Id);
                Property(p => p.BirthDate).HasColumnName("BirthDate").HasColumnType("datetime").IsOptional();
                Property(p => p.ContactTelphoe).HasColumnName("ContactTelphoe").HasColumnType("varchar").HasMaxLength(20).IsOptional();
                Property(p => p.FamilyAddress).HasColumnName("FamilyAddress").HasColumnType("varchar").HasMaxLength(50).IsOptional();
                Property(p => p.Height).HasColumnName("Height").HasColumnType("decimal").IsOptional();
                Property(p => p.Weight).HasColumnName("Weight").HasColumnType("decimal").IsOptional().HasPrecision(6, 2);
                m.Properties(p => new { p.Id, p.BirthDate, p.ContactTelphoe, p.FamilyAddress, p.Weight, p.Height });
                m.ToTable("StudentsDetails");
            });
        }
    }
}